﻿using System;
using System.Collections.Generic;
using System.Text;
using BarCode.Bean;
using BarCode.Common;
using System.Data;
using BarCode.Module;
using MySql.Data.MySqlClient;
using System.Windows.Forms;

namespace BarCode.AccessDao
{
    public class DBSDRCDao
    {
        private static readonly log4net.ILog LogUtil = log4net.LogManager.GetLogger(typeof(DB01Dao));

        private MySqlHelper dbSdrc;
        public DBSDRCDao(string serverIp, string userId, string userPwd)
        {
            dbSdrc = new MySqlHelper(serverIp, "db_sdrc", userId, userPwd);
        }

        /// <summary>
        /// 判断是否可链接
        /// </summary>
        /// <returns></returns>
        public bool EnableConnect()
        {
            return dbSdrc.EnableConnect();
        }

        public List<TBDPurchasemateriallotBean> geTBDLot()
        {
            List<TBDPurchasemateriallotBean> list = new List<TBDPurchasemateriallotBean>();
            DataSet retSet = dbSdrc.GetDataSet(CommandType.Text, "select l.F_SpotOP,l.F_MaterialLot FROM t_bd_purchasemateriallot l where l.F_SpotOP in ('3-OP080','3-OP010','3-OP070','1-OP100','1-OP060-X') ", new MySqlParameter[] {});
            if (retSet != null && retSet.Tables.Count > 0)
            {
                DataTable dt = retSet.Tables[0];
                foreach (DataRow tmpRow in dt.Rows)
                {
                    TBDPurchasemateriallotBean lot = new TBDPurchasemateriallotBean();
                    lot.F_SpotOP = tmpRow["F_SpotOP"].ToString();
                    lot.F_DescExecute = tmpRow["F_DescExecute"].ToString();
                    lot.F_MaterialLot = tmpRow["F_MaterialLot"].ToString();
                    lot.F_WriteAddr = tmpRow["F_WriteAddr"].ToString();
                    lot.F_IsExecute = tmpRow["F_IsExecute"].ToString();
                    lot.F_NameOP = tmpRow["F_NameOP"].ToString();
                    lot.F_MaterialCode = tmpRow["F_MaterialCode"].ToString();
                    list.Add(lot);
                }
            }
            return list;
        }

        /// <summary>
        /// 获取指定用户当日的工作量
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public int getWorkAmount(string userName)
        {
            string amountSql = "select count(*) from t_bd_materialallrecord where F_ReceiveTimeOP080 > date_sub(CONCAT(CURRENT_DATE,' 07:30:00'), interval 1 day)";
            LogUtil.Debug("获取工作量SQL：" + amountSql);
            object amount = dbSdrc.ExecuteScalar(CommandType.Text, amountSql, new MySqlParameter[] { });
            return Convert.ToInt32(amount);
        }

        //查询是否存在
        public int getFCode(string code)
        {
            MySqlParameter param = new MySqlParameter("@F_CoreCode", code);
            string amountSql = "select count(*) FROM t_bd_materialallrecord where F_CoreCode = @F_CoreCode where F_ReceiveTimeOP080 > date_sub(CONCAT(CURRENT_DATE,' 07:30:00'), interval 1 day)";
            LogUtil.Debug("获取工作量SQL：" + amountSql);
            object amount = dbSdrc.ExecuteScalar(CommandType.Text, amountSql, new MySqlParameter[] { param });
            return Convert.ToInt32(amount);
        }
        //修改
        public int editBM(string code, string name, string workShop)
        {
            MySqlConnection conn = null;
            MySqlTransaction uploadBarTrans = null;
            try
            {
                conn = dbSdrc.getConnection();
                conn.Open();
                string updDelbzSql = "update bm_barcode set operation=@operation ,workShop=@workShop,createTime=@createTime where code=@code;";
                MySqlParameter[] sqlParams = new MySqlParameter[4];
                sqlParams[0] = new MySqlParameter("@code", code);
                sqlParams[1] = new MySqlParameter("@operation", name);
                sqlParams[2] = new MySqlParameter("@workShop", workShop);
                sqlParams[3] = new MySqlParameter("@createTime", DateTime.Now);
                int updDelbzCnt = dbSdrc.ExecuteNonQuery(conn, CommandType.Text, updDelbzSql, sqlParams);

            }
            catch (Exception ex)
            {
                LogUtil.Error("异常");
            }
            finally
            {
                try
                {
                    if (conn != null && conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
                catch (Exception e)
                {
                    LogUtil.Error(e);
                }
            }
            return Consts.RET_FAIL;
        }

    }
}

